﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds.My
{
    public partial class Watchlist : MixNP.BusinessLayer.BaseClass
    {
        protected override void OnInit(EventArgs e)
        {
            this.RequiresLogin = true;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindGridViews();
            }

            this.DisplayEmptyDataText();
        }

        private void BindGridViews()
        {
            WatchlistGridView.DataBind();
        }

        private void DisplayEmptyDataText()
        {
            if (WatchlistGridView.Rows.Count.Equals(0))
            {
                Pes.Utility.PageUtility.ExecuteJavaScript("displayHidden", "jQuery(document).ready(function () { $('#no-ad').show(); });", this.Page);
            }
        }

        protected void WatchlistGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName.Equals("Remove"))
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                double itemId = Pes.Utility.Conversion.TryCastDouble(row.Cells[1].Text);
                MixNP.BusinessLayer.Items.RemoveFromWatchlist(User.Identity.Name, itemId);
                this.BindGridViews();
                this.DisplayEmptyDataText();
            }
        }

        protected void WatchlistGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                double itemId = Pes.Utility.Conversion.TryCastInteger(e.Row.Cells[1].Text);
                string itemTitle = e.Row.Cells[2].Text;
                e.Row.Cells[2].Text = "<a class='item' target='_blank' href='" + MixNP.BusinessLayer.Items.GetItemUrl(itemId, this.Page) + "' title='" + itemTitle + "'>" + itemTitle + "</a>";
            }
        }

    }
}